package helpers.other
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;

	public class AutoAddHtmlText extends EventDispatcher
	{
		private var _curIndex:int;
		private var _totalIndex:int;
		private var _playlist:Array;
		
		public function AutoAddHtmlText(text:String="",_interval:int = 150)
		{
			reset();
			setText(text);
			interval = _interval;
		}
		
		public function reset():void
		{
			_curIndex = -1;
			_totalIndex = 0;
			_curText = "";
		}
		
		private var text:String;
		
		public function setText(value:String):void
		{
			text = value;
			_playlist = [];
			if(text != null && text.length>0){
				var xml:XML = new XML('<div>'+text+'</div>');
				addLength(xml);
				var str:String = _playlist[0];
				_playlist[0] = str.substr(str.indexOf('>')+1);
				str = _playlist[_playlist.length-1];
				_playlist[_playlist.length-1] = str.substr(0,str.lastIndexOf('<'));
			}
		}
		
		private function addLength(xml:XML):void
		{
			var index:int = 0;
			for each(var node:XML in xml.children()){
				if(!node.hasComplexContent()){
					_totalIndex+=String(node).length;
					var i:int=0;
					var nodestr:String = '';
					if(node.nodeKind() == "text"){
						for(;i<node.toString().length;i++){
							nodestr = '';
							if(index == 0 && i == 0){
								nodestr += xml.toXMLString().substr(0,xml.toXMLString().indexOf('>')+1);
							}
							nodestr += node.toString().charAt(i);
							if(index == xml.children().length()-1 && i == node.toString().length-1){
								nodestr += xml.toXMLString().substr(xml.toXMLString().lastIndexOf('<'));
							}
							_playlist.push(nodestr);
						}
					} else if(node.nodeKind() == "element") {
						i=0;
						for(;i<String(node).length;i++){
							nodestr = '';
							if(i == 0){
								if(index == 0)
									nodestr += xml.toXMLString().substr(0,xml.toXMLString().indexOf('>')+1);
								nodestr += node.toXMLString().substr(0,node.toXMLString().indexOf('>')+1);
							}
							nodestr += node.toString().charAt(i);
							if(i == node.toString().length-1){
								nodestr += node.toXMLString().substr(node.toXMLString().lastIndexOf('<'));
								if(index == xml.children().length()-1)
									nodestr += xml.toXMLString().substr(xml.toXMLString().lastIndexOf('<'));
							}
							_playlist.push(nodestr);
						}
					}
				} else {
					addLength(node);
				}
				index++;
			}
		}
		
		public function getText():String
		{
			return text;
		}
		
		private var _curText:String;
		
		public function get curText():String
		{
			return _curText;
		}
		
		private var _interval:int;
		
		public function set interval(value:int):void
		{
			_interval = value;
		}
		
		public function get interval():int
		{
			return _interval;
		}
		
		public function get isPlaying():Boolean
		{
			return _intervalId>0;
		}
		
		private var _intervalId:int = 0;
		
		public function play():void
		{
			if(isPlaying){
				this.stop();
			}
			_intervalId = setInterval(render,this._interval);
		}
		
		private function render():void
		{
			_curIndex++;
			if(_curIndex>_totalIndex-1){
				this.stop();
				return;
			}
			_curText += this._playlist[_curIndex];
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function stop():void
		{
			if(_intervalId>0)
				clearInterval(_intervalId);
			this.reset();
		}
		
		public function pause():void
		{
			if(_intervalId>0)
				clearInterval(_intervalId);
			_intervalId = 0;
		}
	}
}